package com.lujin.eduservice.controller;


import com.lujin.commonutils.R;
import com.lujin.eduservice.client.VodClient;
import com.lujin.eduservice.entity.EduVideo;
import com.lujin.eduservice.service.EduVideoService;
import com.lujin.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author lujin
 * @since 2021-07-06
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    //注入
    @Autowired
    private VodClient vodClient;
    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return R.ok();
    }

    //删除小节时，同时把里面的视频也删除
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id){
        //根据小节id得到视频id，调用方法实现删除
        EduVideo eduVideo = videoService.getById(id);
        String videoSourceId=eduVideo.getVideoSourceId();
        //判断小节是否有视频id
        if (!StringUtils.isEmpty(videoSourceId)){
            //根据视频id，远程调用service_vod中的方法实现删除
            R result = vodClient.removeAlyVideo(videoSourceId);
            if (result.getCode()==20001){
                throw new GuliException(20001,"删除视频失败，熔断器...");
            }
        }
        //删除小节
        videoService.removeById(id);
        return R.ok();
    }
    //修改小节

}

